#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>

#define TASK_NUM 10

//预设一批任务
void sync_disk(){
  printf("这是一个刷新数据的任务!\n");
}

void sync_log(){
  printf("这是一个同步日志的任务!\n");
}

void net_send(){
  printf("这是一个进行网络发送的任务!\n");
}

//要保存的任务相关的
typedef void (*func_t)();
func_t other_task[TASK_NUM] = {NULL};

int LoadTask(func_t func){
  int i = 0;
  for (i = 0; i < TASK_NUM; i++){
    if (other_task[i] == NULL){
      break;
    }
  }
  if (i == TASK_NUM){
    return -1;
  }else{
    other_task[i] = func;
  }
  return 0;
}
void InitTask(){
  int i = 0;
  for (; i < TASK_NUM; i++){
    other_task[i] = NULL;
  }
  LoadTask(sync_disk);
  LoadTask(sync_log);
  LoadTask(net_send);
}

void RunTask(){
  int i = 0;
  for (; i < TASK_NUM; i++){
    if (other_task[i] == NULL) continue;
    other_task[i]();
  }
}
int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        //子进程
        int cnt = 50;
        while(cnt)
        {
            printf("I  am child process. I have %d seconds to exit. pid: %d, ppid: %d\n", cnt--, getpid(), getppid());
            sleep(1);
            //bint* p = NULL;
           //p = 100;
        }
        exit(111);
    } 
    InitTask();
    // 父进程
    while(1)
    {
        int status = 0;
        pid_t ret_id = waitpid(id, &status, WNOHANG); // 夯住了
        if(ret_id < 0)
        {
            printf("waitpid error!\n");
            exit(1);
        }
        else if(ret_id == 0)
        {
            RunTask();
            sleep(1);
            continue;
        }
        else{
          //是否收到信号
          if (WIFEXITED(status)){
            printf("wait sucess, child exit code: %d\n", WEXITSTATUS(status));
          }else{
            printf("wait sucess, child exit signal: %d\n", status & 0x7F);
          }
//            printf("I am father process. Wait sucess!!! pid: %d, ppid: %d, ret_id: %d, child exit code: %d, child exit signal: %d\n",\
//                getpid(), getppid(), ret_id, (status>>8)&0xFF, status & 0x7F);
            break;
        }
    }
}
